<?php
namespace app\admin\builder\src;
use app\admin\builder\MBuilder;

class Form extends MBuilder
{
    protected $config = [
        'action'=>'',
        'formrow'=>[],
        'button'=>[

        ]
    ];

    /**设置form表单请求的地址
     * @param string $action
     * @return $this
     */
    public function setAction(string $action=''){
        if(!empty($action)){
            $this->config['action'] = $action;
        }
        return $this;
    }

    /**设置form表单的一列
     * @param array表单一列的参数 $array
     * @return $this
     */
    public function setFormRow(array $array=[]){
        $this->config['formrow'][] = $array;
        return $this;
    }

    /**添加隐藏框
     * @param string提交字段名 $name
     * @param string默认值 $value
     * @return $this
     */
    public function setNone(string $name,string $value=''){
        $this->config['formrow'][] = [
            'type'=>'none',
            'name'=>$name,
            'value'=>$value,
            'template'=>'../app/admin/view/builder/form/none.html',
        ];
        return $this;
    }

    /**设置input框
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string默认值 $value
     * @param string标识 $tagging
     * @return $this
     */
    public function setInput(string $desc,string $name,string $value='',string $tagging=''){
        $this->config['formrow'][] = [
            'type'=>'input',
            'desc'=>$desc,
            'name'=>$name,
            'value'=>$value,
            'tagging'=>$tagging,
            'template'=>'../app/admin/view/builder/form/input.html',
        ];
        return $this;
    }
    /**设置多行文本
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string默认值 $value
     * @param string标识 $tagging
     * @return $this
     */
    public function setTextarea(string $desc,string $name,string $value='',string $tagging=''){
        $this->config['formrow'][] = [
            'type'=>'textarea',
            'desc'=>$desc,
            'name'=>$name,
            'value'=>$value,
            'tagging'=>$tagging,
            'template'=>'../app/admin/view/builder/form/editor.html',
        ];
        return $this;
    }
    /**设置单文件域
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string默认值 $value
     * @param string可上传类型 $type
     * @param string标识 $tagging
     * @return $this
     */
    public function setFile(string $desc,string $name,string $value='',string $type='png|jpg|ico|jpeg|apk|xls|xlsx|txt|doc|docx|ppt|pptx|video',string $tagging=''){
        $this->config['formrow'][] = [
            'type'=>'file',
            'desc'=>$desc,
            'name'=>$name,
            'value'=>$value,
            'tagging'=>$tagging,
            'filetype'=>$type,
            'show'=>$value,
            'template'=>'../app/admin/view/builder/form/file.html',
        ];
        return $this;
    }
    /**设置多文件域
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string默认值 $value
     * @param string可上传类型 $type
     * @param string标识 $tagging
     * @return $this
     */
    public function setFiles(string $desc,string $name,string $value='',string $type='png|jpg|ico|jpeg|apk|xls|xlsx|txt|doc|docx|ppt|pptx|video',string $tagging=''){
        $show = explode(',',$value);
        if(count($show)==1){
            $show = explode('|',$show[0]);
        }
        $this->config['formrow'][] = [
            'type'=>'files',
            'desc'=>$desc,
            'name'=>$name,
            'value'=>$value,
            'tagging'=>$tagging,
            'filetype'=>$type,
            'show'=>$show,
            'template'=>'../app/admin/view/builder/form/files.html',
        ];
        return $this;
    }
    /**设置单图片
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string默认值 $value
     * @param string可上传类型 $type
     * @param string标识 $tagging
     * @return $this
     */
    public function setImg(string $desc,string $name,string $value='',string $type='image/*',string $tagging=''){
        $this->config['formrow'][] = [
            'type'=>'file',
            'desc'=>$desc,
            'name'=>$name,
            'value'=>$value,
            'tagging'=>$tagging,
            'filetype'=>$type,
            'show'=>$value,
            'template'=>'../app/admin/view/builder/form/img.html',
        ];
        return $this;
    }
    /**设置多图片
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string默认值 $value
     * @param string可上传类型 $type
     * @param string标识 $tagging
     * @return $this
     */
    public function setImgs(string $desc,string $name,string $value='',string $type='image/*',string $tagging=''){
        $show = explode(',',$value);
        if(count($show)==1){
            $show = explode('|',$show[0]);
        }
        $this->config['formrow'][] = [
            'type'=>'file',
            'desc'=>$desc,
            'name'=>$name,
            'value'=>$value,
            'tagging'=>$tagging,
            'filetype'=>$type,
            'show'=>$show,
            'template'=>'../app/admin/view/builder/form/img.html',
        ];
        return $this;
    }
    /**设置视频
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string默认值 $value
     * @param string可上传类型 $type
     * @param string标识 $tagging
     * @return $this
     */
    public function setVideo(string $desc,string $name,string $value='',string $type='*',string $tagging=''){
        $this->config['formrow'][] = [
            'type'=>'video',
            'desc'=>$desc,
            'name'=>$name,
            'value'=>$value,
            'tagging'=>$tagging,
            'filetype'=>$type,
            'show'=>$value,
            'template'=>'../app/admin/view/builder/form/img.html',
        ];
        return $this;
    }
    /**添加单选框
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param array选项 $option
     * @param string默认值 $value
     * @param string二维数组显示的字段 $title
     * @param string二维数组选择的字段 $choice
     * @param string标识 $tagging
     * @return $this
     */
    public function setRadio(string $desc,string $name,array $option=[],string $value='',string $title='name',string $choice='id',string $tagging=''){
        $dimension = 1;
//        dump($option[0]);exit;
        if(isset($option[0])&&is_array($option[0])){
            $dimension = 2;
        }
        $this->config['formrow'][] = [
            'type'=>'radio',
            'desc'=>$desc,
            'name'=>$name,
            'option'=>$option,
            'value'=>$value,
            'dimension'=>$dimension,
            'tagging'=>$tagging,
            'title'=>$title,
            'choice'=>$choice,
            'template'=>'../app/admin/view/builder/form/checkbox.html',
        ];
        return $this;
    }
    /**添加多选框
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param array选项 $option
     * @param string默认值 $value
     * @param string二维数组显示的字段 $title
     * @param string二维数组选择的字段 $choice
     * @param string标识 $tagging
     * @return $this
     */
    public function setCheckbox(string $desc,string $name,array $option=[],$value='',string $title='name',string $choice='id',string $tagging=''){
        if(!is_array($value)){
            $value = explode(',',$value);
            if(count($value) ==1){
                $value = explode('|',$value[0]);
            }
        }
        $dimension = 1;
        if(isset($option[0])){
            if(isset($option[0])&&is_array($option[0])){
                $dimension = 2;
            }else{
                $dimension = 0;
            }
        }else{
            $dimension = 1;
        }
        $this->config['formrow'][] = [
            'type'=>'checkbox',
            'desc'=>$desc,
            'name'=>$name,
            'option'=>$option,
            'value'=>$value,
            'dimension'=>$dimension,
            'tagging'=>$tagging,
            'title'=>$title,
            'choice'=>$choice,
            'template'=>'../app/admin/view/builder/form/checkbox.html',
        ];
        return $this;
    }
    /**添加下拉框
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param array选项 $option
     * @param string默认值 $value
     * @param string二维数组显示的字段 $title
     * @param string二维数组选择的字段 $choice
     * @param string标识 $tagging
     * @return $this
     */
    public function setSelect(string $desc,string $name,array $option=[],string $value='',string $title='name',string $choice='id',string $tagging=''){
        $dimension = 1;
//        dump($option[0]);exit;
        if(isset($option[0])&&is_array($option[0])){
            $dimension = 2;
        }
        $this->config['formrow'][] = [
            'type'=>'select',
            'desc'=>$desc,
            'name'=>$name,
            'option'=>$option,
            'value'=>$value,
            'dimension'=>$dimension,
            'tagging'=>$tagging,
            'title'=>$title,
            'choice'=>$choice,
            'template'=>'../app/admin/view/builder/form/checkbox.html',
        ];
        return $this;
    }
    /**设置颜色选择框
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string默认值 $value
     * @param string标识 $tagging
     * @return $this
     */
    public function setColor(string $desc,string $name,string $value='',string $tagging=''){
        $this->config['formrow'][] = [
            'type'=>'color',
            'desc'=>$desc,
            'name'=>$name,
            'value'=>$value,
            'tagging'=>$tagging,
            'template'=>'../app/admin/view/builder/form/color.html',
        ];
        return $this;
    }
    /**设置编辑框
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string默认值 $value
     * @param string标识 $tagging
     * @param string高度 $height
     * @return $this
     */
    public function setEditor(string $desc,string $name,string $value='',string $tagging='',string $height='500px'){
        $this->config['formrow'][] = [
            'type'=>'editor',
            'desc'=>$desc,
            'name'=>$name,
            'value'=>$value,
            'tagging'=>$tagging,
            'height'=>$height,
            'template'=>'../app/admin/view/builder/form/color.html',
        ];
        return $this;
    }
    /**设置密码框
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string默认值 $value
     * @param string标识 $tagging
     * @return $this
     */
    public function setPassword(string $desc,string $name,string $value='',string $tagging=''){
        $this->config['formrow'][] = [
            'type'=>'password',
            'desc'=>$desc,
            'name'=>$name,
            'value'=>$value,
            'tagging'=>$tagging,
            'template'=>'../app/admin/view/builder/form/color.html',
        ];
        return $this;
    }
    /**设置开关
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string开关选项 $optiuon
     * @param string默认值 $value
     * @param string标识 $tagging
     * @return $this
     */
    public function setSwitch(string $desc,string $name,$option='开启|关闭',string $value='',string $tagging=''){
        $this->config['formrow'][] = [
            'type'=>'switch',
            'desc'=>$desc,
            'name'=>$name,
            'option'=>$option,
            'value'=>$value,
            'tagging'=>$tagging,
            'template'=>'../app/admin/view/builder/form/color.html',
        ];
        return $this;
    }
    /**设置日期框
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string默认值 $value
     * @param string标识 $tagging
     * @return $this
     */
    public function setDate(string $desc,string $name,string $value='',string $tagging=''){
        $this->config['formrow'][] = [
            'type'=>'date',
            'desc'=>$desc,
            'name'=>$name,
//            'option'=>$optiuon,
            'value'=>$value,
            'tagging'=>$tagging,
            'template'=>'../app/admin/view/builder/form/color.html',
        ];
        return $this;
    }
    /**设置日期时间框
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string默认值 $value
     * @param string标识 $tagging
     * @return $this
     */
    public function setDateTime(string $desc,string $name,string $value='',string $tagging=''){
        $this->config['formrow'][] = [
            'type'=>'datetime',
            'desc'=>$desc,
            'name'=>$name,
//            'option'=>$optiuon,
            'value'=>$value,
            'tagging'=>$tagging,
            'template'=>'../app/admin/view/builder/form/color.html',
        ];
        return $this;
    }
    /**设置时间框
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string默认值 $value
     * @param string标识 $tagging
     * @return $this
     */
    public function setTime(string $desc,string $name,string $value='',string $tagging=''){
        $this->config['formrow'][] = [
            'type'=>'time',
            'desc'=>$desc,
            'name'=>$name,
//            'option'=>$optiuon,
            'value'=>$value,
            'tagging'=>$tagging,
            'template'=>'../app/admin/view/builder/form/color.html',
        ];
        return $this;
    }
    /**设置json数组组件
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string默认值 $value
     * @param string标识 $tagging
     * @return $this
     */
    public function setJsonArray(string $desc,string $name,$value='',string $tagging=''){
        $index = 0;
        if(!empty($value)){
            if(!is_array($value)){
                $value = json_decode($value,true);
            }
            if(isset($value['key'])){
                $index = count($value['key'])-1;
            }
        }
        $this->config['formrow'][] = [
            'type'=>'jsonarray',
            'desc'=>$desc,
            'name'=>$name,
//            'option'=>$optiuon,
            'value'=>$value,
            'index'=>$index,
            'tagging'=>$tagging,
            'template'=>'../app/admin/view/builder/form/color.html',
        ];
        return $this;
    }
    /**设置树状组件
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param string获取数据接口 $action
     * @param string默认值 $value
     * @param string标识 $tagging
     * @param string显示字段 $title
     * @param string提交字段 $choice
     * @return $this
     */
    public function setTree(string $desc,string $name,string $action='',$value='',string $tagging='',string $title='name',string $choice='id'){

        $this->config['formrow'][] = [
            'type'=>'tree',
            'desc'=>$desc,
            'name'=>$name,
            'action'=>$action,
            'value'=>$value,
            'tagging'=>$tagging,
            'title'=>$title,
            'choice'=>$choice,
            'template'=>'../app/admin/view/builder/form/color.html',
        ];
        return $this;
    }

    /**设置下拉组
     * @param string字段名称 $desc
     * @param string提交字段名 $name
     * @param array数据 $option
     * @param string默认值 $value
     * @param string标识 $tagging
     * @param string显示字段 $title
     * @param string提交字段 $choice
     * @return $this
     */
    public function setSelectGroup(string $desc,string $name,array $option=[],$value='',string $tagging='',string $title='name',string $choice='id'){
        $this->config['formrow'][] = [
            'type'=>'selectgroup',
            'desc'=>$desc,
            'name'=>$name,
            'option'=>$option,
            'value'=>$value,
            'tagging'=>$tagging,
            'title'=>$title,
            'choice'=>$choice,
            'template'=>'../app/admin/view/builder/form/color.html',
        ];
        return $this;
    }

    /**添加提交按钮
     * @return $this
     */
    public function setSubmitButton(){
        $this->config['button']['submit'] = true;
        return $this;
    }
    public function setResetButton(){
        $this->config['button']['reset'] = true;
        return $this;
    }
    public function fetch(string $emplate = '')
    {
//        echo '<pre>';
//        print_r($this->config);
//        exit;
        return view(app_path().'/view/builder/form/index.html',['config'=>$this->config,'thisControllerJsPath'=>'admin/js/builder/form.js','thisAction'=>'edit','formname'=>'ces.html']); // TODO: Change the autogenerated stub
    }

}